package com.songcha.module_bookshelf.ui.adapter

import android.content.Context
import android.view.View
import androidx.databinding.ViewDataBinding
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.songcha.library_business.enums.BookDownloadStatus
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.util.GlideUtil
import com.songcha.library_common.util.StringUtil
import com.songcha.module_bookshelf.R
import com.songcha.module_bookshelf.databinding.BookshelfItemBookshelfBinding
import com.songcha.module_bookshelf.structs.BookShelfItem

class BookShelfAdapter<DB:ViewDataBinding>(list:MutableList<BookShelfItem>, var mIsManage:Boolean, val mListener:IOnBookShelfItemCheckListener):BaseQuickAdapter<BookShelfItem,BaseDataBindingHolder<DB>>(R.layout.bookshelf_item_bookshelf,list) {

    interface IOnBookShelfItemCheckListener{
        fun onBookShelfItemCheck(isCheck:Boolean)
    }

    fun setIsManage(isManage:Boolean){
        this.mIsManage=isManage
        notifyDataSetChanged()
    }

    fun setSelectItem(item:BookShelfItem, isCheck:Boolean){
        item.isSelect=isCheck
        mListener.onBookShelfItemCheck(isCheck)
    }


    override fun convert(holder: BaseDataBindingHolder<DB>, item: BookShelfItem) {
        val binding=holder.dataBinding as BookshelfItemBookshelfBinding
        binding.bookshelfTvBookName.text=item.bookName
        GlideUtil.loadNetImage(binding.bookshelfIvThumb,item.thumb)
        if(item.progress>0){
            if(item.progress>=100){
                binding.bookshelfTvProgress.text="已读完"
                binding.bookshelfTvProgressValue.text=""
            }else{
                binding.bookshelfTvProgress.text="读到"
                binding.bookshelfTvProgressValue.text="${StringUtil.float2PlainString(item.progress)}%"
            }
        }else{
            binding.bookshelfTvProgress.text="未读"
            binding.bookshelfTvProgressValue.text=""
        }

        if(item.downloadStatus==BookDownloadStatus.NO_DOWNLOAD){
            binding.bookshelfCclDownloadStatus.visibility=View.GONE
        }else{
            binding.bookshelfCclDownloadStatus.visibility=View.VISIBLE
            if(item.downloadStatus==BookDownloadStatus.DOWNLOADING){
                binding.bookshelfTvDownloadStatus.text="缓存中"
            }else if(item.downloadStatus==BookDownloadStatus.DOWNLOADED){
                binding.bookshelfTvDownloadStatus.text="已缓存"
            }
        }

        ViewHelper.setVisibility(mIsManage,binding.bookshelfCcb)
        if(mIsManage){
            binding.bookshelfCcb.setIsCheck(item.isSelect)
        }
        binding.bookshelfCcb.setOnCheckListener{isCheck->
            setSelectItem(item,isCheck)
        }
    }

}